#pragma once
#include <windows.h>
#include <array>
#include <string>
#include "physics.h"

enum class Preset { FigureEight, Solar, FigureEightLoose };
#include "renderer_win32.h"

struct App {
    HWND hwnd{}; RendererWin32 r; State s; PhysParams pp{1.0,1e-3,1e-3}; View view{300.0, 640.0, 360.0}; bool paused=false; bool showTrail=true; std::array<Trail,3> trails; double energy0{};
    double accumulator{0.0}; int maxStepsPerFrame{8}; int trailSkip{2}; int trailMax{1200}; int tick{0}; bool followCOM{true}; Preset preset{Preset::FigureEightLoose};
    double looseTheta{0.05}; double loosePosScale{1.03}; double looseVelScale{0.97};
    double baseDt{8e-4}; bool adaptiveDt{true}; double dtMinScale{0.15}; double minRRef{0.15};
    bool autoScale{true}; double autoScaleMargin{0.45};
    double simTime{0.0}; bool autoRandomize{true}; double reRandInterval{10.0}; double farThreshold{4.0}; double posRand{0.02}; double velRand{0.02}; unsigned int rngSeed{5489u};
    void init(HWND h, int w, int hgt);
    void reset();
    void update(double dt);
    void render();
    void on_size(int w, int hgt);
    void on_key(WPARAM k);
    void on_wheel(short d, int x, int y);
    void on_mouse_down(int x, int y);
    void on_mouse_move(int x, int y, WPARAM flags);
    void clear_trails();
private:
    bool dragging=false; POINT last{};
};